﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMS
{
    public partial class frmJFmanage : Form
    {
        DBOperate operate = new DBOperate();
        public frmJFmanage()
        {
            InitializeComponent();
        }

        private void frmJFmanage_Load(object sender, EventArgs e)
        {
            SqlConnection conn = DBConnection.MyConnection();                   //创建数据库连接对象
            conn.Open();                                                        //打开数据库连接
            SqlCommand cmd = new SqlCommand("select * from tb_employee", conn); //创建数据库命令对象
            SqlDataReader sdr = cmd.ExecuteReader();                            //得到数据读取器
            while(sdr.Read())
            {
                cbbUserNum.Items.Add(sdr["employeeID"].ToString());             //添加数据项
            }
            sdr.Close();                                                        //关闭数据读取器
            conn.Close();                                                       //关闭数据库连接
        }

        private void cbbUserNum_SelectIndexChange(object sender,EventArgs e)
        {
            string str = "select * from tb_employee where employeeID='" + cbbUserNum.Text + "'";    //创建SQL字符串
            DataSet ds = operate.GetTable(str);                                                     //得到数据集
            ds.Dispose();                                                                           //释放资源
            txtname.Text = ds.Tables[0].Rows[0][2].ToString();                                      //得到用户名
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(cbbUserNum.Text == ""|| cbbUserJFType.Text == ""||
                txtJFcontent.Text == ""|| txtJFdate.Text == ""||
                txtCXDate.Text =="")
            {
                MessageBox.Show("请将信息填写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                if(cbbUserJFType.Text == "奖励")
                {
                    if(txtJL.Text == "")
                    {
                        MessageBox.Show("请输入奖励金额!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        string sql =
                            "select count(*) from tb_prize where UserID='" + cbbUserNum.Text + "'";
                        int i = operate.HumanNum(sql);
                        if(i > 0)
                        {
                            MessageBox.Show("员工编号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        else
                        {
                            string str =
                                "insert into tb_prize(UserID,UserName,UserDep,UserJF,UserJFContent,UserJLMoney,UserFKMoney,UserJAFDate,UserCXDate) values('" + cbbUserNum.Text + "','" + txtname.Text + "','" +
                                txtdep.Text + "','" + cbbUserJFType.Text + "','" + txtJFcontent.Text + "','" + txtJL.Text + "','" + txtFK.Text + "','" + txtCXDate.Text + "')";
                            operate.OperateData(str);                                                                                           
                            MessageBox.Show("添加成功!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        }
                    }
                }
                else
                {
                    try
                    {
                        if(txtFK.Text == "")
                        {
                            MessageBox.Show("罚款金额不为空");
                        }
                        else
                        {
                            string sql =
                                "select count(*) from tb_prize where UserID='" + cbbUserNum.Text + "'";
                            int i = operate.HumanNum(sql);
                            if(i>0)
                            {
                                MessageBox.Show("员工编号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                            else
                            {
                                string str =
                                    "insert into tb_prize(UserID,UserName,UserDep,UserJF,UserJFcontent,UserJLMoney,UserFKMoney,UserJFDate,UserCXDate) values('" + cbbUserNum.Text + "','" +
                                    txtname.Text + "','" + txtdep.Text + "','" + cbbUserJFType.Text + "','" + txtJFcontent.Text + "','" + txtJL.Text + "','" +
                                    txtFK.Text + "','" + txtJFdate.Text + "','" + txtCXDate.Text + "')";
                                operate.OperateData(str);
                                MessageBox.Show("添加成功!", "提示",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void cbbUserJFType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbbUserJFType.Text == "奖励")
            {
                txtFK.Enabled = false;
                txtFK.Text = "0";
                txtJL.Enabled = true;
            }
            else
            {
                txtJL.Enabled = false;
                txtJL.Text = "0";
                txtFK.Enabled = true;
            }
        }
    }
}
